#include "modbus_register.h"
#include "ice_cream_process.h"

// 冰淇淋设置模块私有变量
static uint16_t manual_speed[2] = {0};        // 手动速度XZ轴
static uint16_t ice1_remain_count = 0;        // 冰淇淋一剩余出杯数
static uint16_t remain_after_shortage = 0;    // 原料欠料后可售杯数
static uint16_t total_disp_time[2] = {0};     // 原料总出料时间一、二
static uint16_t manual_cup_disp = 0;          // 手动下杯

// 冰淇淋设置模块读取处理
uint16_t modbus_ice_cream_get(uint16_t address) {

  const ice_cream_process_data_t *process_data=  ice_icream_process_data_get();
    // 取杯位置XZ轴(数组[2])
    if (address == REG_CUP_PICKUP_POS_BASE) {
        return process_data->config.cup_drop_pos.x_pos;
    } else if (address == REG_CUP_PICKUP_POS_BASE + 1) {
        return process_data->config.cup_drop_pos.z_pos;
    }
    
    // 原料位置XZ轴(数组[2])
    else if (address == REG_ING_POS_BASE) {
       return process_data->config.milk.fill_pos.x_pos;
    } else if (address == REG_ING_POS_BASE + 1) {
        return process_data->config.milk.fill_pos.z_pos;
    }
    
    // 出杯位置XZ轴(数组[2])
    else if (address == REG_CUP_DISP_POS_BASE) {
        return process_data->config.dispense_pos.x_pos;
    } else if (address == REG_CUP_DISP_POS_BASE + 1) {
        return process_data->config.dispense_pos.z_pos;
    }
    
    // 手动速度XZ轴(数组[2])
    else if (address == REG_MANUAL_SPEED_BASE) {
        return manual_speed[0]; // X轴速度
    } else if (address == REG_MANUAL_SPEED_BASE + 1) {
        return manual_speed[1]; // Z轴速度
    }
    
    // 冰淇淋一剩余出杯数
    else if (address == REG_ICE1_REMAIN_COUNT) {
        return process_data->config.milk.remain_cup_count;
    }
    
    // 原料欠料后可售杯数
    else if (address == REG_REMAIN_AFTER_SHORTAGE) {
       return process_data->config.milk.material_low_remaining_cups;
    }
    
    // 原料总出料时间一、二(数组[2])
    else if (address == REG_TOTAL_DISP_TIME_BASE) {
        return (process_data->config.milk.fill_time_ms/1000);
    } else if (address == REG_TOTAL_DISP_TIME_BASE + 1) 
    {
        return 0; // 时间二
    }
    
    // 手动下杯
    else if (address == REG_MANUAL_CUP_DISP) {
        return manual_cup_disp;
    }
    
    return 0; // 未匹配的地址
}

// 冰淇淋设置模块写入处理
void modbus_ice_cream_set(uint16_t address, uint16_t value) {
   ice_cream_process_data_t *process_data=  ice_icream_process_data_get();
// 取杯位置XZ轴(数组[2])
    if (address == REG_CUP_PICKUP_POS_BASE) {
         process_data->config.cup_drop_pos.x_pos=(int16_t)value;
    } else if (address == REG_CUP_PICKUP_POS_BASE + 1) {
         process_data->config.cup_drop_pos.z_pos=(int16_t)value  ;
    }
    // 原料位置XZ轴(数组[2])
    else if (address == REG_ING_POS_BASE) {
        process_data->config.milk.fill_pos.x_pos=(int16_t)value;
    } else if (address == REG_ING_POS_BASE + 1) {
         process_data->config.milk.fill_pos.z_pos=(int16_t)value;
    }
    // 出杯位置XZ轴(数组[2])
    else if (address == REG_CUP_DISP_POS_BASE) {
         process_data->config.dispense_pos.x_pos = (int16_t)value;
    } else if (address == REG_CUP_DISP_POS_BASE + 1) {
         process_data->config.dispense_pos.z_pos = (int16_t)value;
    }
    
    // 手动速度XZ轴(数组[2])
    else if (address == REG_MANUAL_SPEED_BASE) {
        manual_speed[0] = value; // X轴速度
    } else if (address == REG_MANUAL_SPEED_BASE + 1) {
        manual_speed[1] = value; // Z轴速度
    }
    
    // 冰淇淋一剩余出杯数
    else if (address == REG_ICE1_REMAIN_COUNT) {
       process_data->config.milk.remain_cup_count=value;
    }
    
    // 原料欠料后可售杯数
    else if (address == REG_REMAIN_AFTER_SHORTAGE) {
        process_data->config.milk.material_low_remaining_cups =value;
    }
    
    // 原料总出料时间一、二(数组[2])
    else if (address == REG_TOTAL_DISP_TIME_BASE) {
        process_data->config.milk.fill_time_ms=value*1000;
    } else if (address == REG_TOTAL_DISP_TIME_BASE + 1) {

    }
    
    // 手动下杯
    else if (address == REG_MANUAL_CUP_DISP) {
        manual_cup_disp = value;
    }
}
    